#include "analog.h"

void AnalogBegin(uint8_t mode) {
	ADMUX = (mode << REFS0);
	ADCSRA = _BV(ADEN) | _BV(ADPS0) | _BV(ADPS1);
}

int AnalogRead(uint8_t channel, uint8_t read_times) {
	long total = 0;
	ADMUX = (ADMUX & 0xF0) | (channel & 0x0F);

	for(int i=0; i < read_times; i++) {
		// Tell the microcontroller to start conversion
		ADCSRA |= _BV(ADSC);

		while ((ADCSRA & _BV(ADIF)) == 0);
		unsigned char low_byte, high_byte;
		low_byte = ADCL;
		high_byte = ADCH;

		// Clear the ADIF bit so that ADC is ready for the next conversion
		ADCSRA |= _BV(ADIF);

		// Return the value read
		total += (int) (high_byte * 255 + low_byte);
	}
	return (int) total/read_times;
}

void AnalogWrite(uint8_t pin, uint8_t value) {
	switch(pin) {
		case 3:
			TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
			TCCR2B = _BV(CS22);
			OCR2B = value;
			break;
		case 5:
			TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
			TCCR0B = _BV(CS02);
			OCR0B = value;
			break;
		case 6:
			TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM01) | _BV(WGM00);
			TCCR0B = _BV(CS02);
			OCR0A = value;
			break;
		case 9:
			TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11) | _BV(WGM10);
			TCCR1B = _BV(CS12);
			OCR1A = value;
			break;
		case 10:
			TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11) | _BV(WGM10);
			TCCR1B = _BV(CS12);
			OCR1B = value;
			break;
		case 11:
			TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
			TCCR2B = _BV(CS22);
			OCR2A = value;
			break;
	}
}
